<?php
session_start();

include_once __DIR__."/../public/db.class.php";
class Load
{
	public static function load_class($class_name)
	{
		$filename = "../public/".$class_name.".class.php";
		if(is_file($filename))
		{
			return include $filename;
		}
	}
}

spl_autoload_register("Load::load_class");

function login()
{
	$user = new User();
	if($user -> is_legal($_POST['username'], $_POST['password']) === false)
	{
		throw new Exception("login failed!");
	}
	else
	{
		$id = $user -> get_user_id($_POST['username']);
		$user -> set_online($id);
		$_SESSION["username"] = $_POST['username'];
		$_SESSION["user_id"] = $id;
	}
	return array('msg' => 'ok');
}

function load_message()
{
	$time_loop = 0;
	while(true)
	{
		$time_loop ++;
		if($time_loop === 3)
		{
			throw new Exception("time out!");
		}
		if(!isset($_SESSION['username']) || !isset($_SESSION['user_id']))		
		{
			throw new Exception("login please!");
		}
		else
		{
			$message = new Message($_SESSION['user_id']);
			$mes_id = $message -> get_msg_id($_SESSION['user_id']);
			$result = $message -> get_message($mes_id);
			$user = new User();
			$online_sum = $user::$user_online;

			//'2' == 2
			if($result === false && ($online_sum == $user -> get_user_online_sum($_SESSION['user_id'])))
			{
				// echo json_encode(array('result' => true, 'msg' => '233333'));
				sleep(1);
				/* throw new Exception("no new message!"); */
			}
			else
			{
				$new_msg = array();
				if($result !== false)
				{
					$i = 0;
					foreach($result as $msg)
					{
						$new_msg[$i]['from'] = $msg['mes_to'];
						$new_msg[$i]['username'] = $msg['username'];
						$new_msg[$i]['content'] = $msg['mes_content'];
						$new_msg[$i]['pubdate'] = $msg['mes_pubdate'];
						$i ++;
					}
				}
				$online = array();
				$i = 0;
				$online_result = $user -> get_online_user();
				foreach($online_result as $u)
				{
					$online[$i]['username'] = $u['username'];
					$i ++;
				}
				$user -> set_user_online_sum($_SESSION['user_id'], $online_sum);


				return array('username' => $_SESSION['username'], 'message' => $new_msg, 'users' => $online);
			}
			/* } */
		}
	}
}

/* function get_online_users() */
/* { */
/* 	if(!isset($_SESSION['username']) || !isset($_SESSION['user_id'])) */		
/* 	{ */
/* 		throw new Exception("login please!"); */
/* 	} */
/* 	else */
/* 	{ */
/* 		$user = new User(); */
/* 		$result = $user -> get_online_user(); */
/* 		$user_list = array(); */
/* 		$i = 0; */
/* 		foreach($result as $msg) */
/* 		{ */
/* 			$user_list[$i]['username'] = $msg['username']; */
/* 			$i ++; */
/* 		} */
/* 		sleep(5); */

/* 		return array("username" => $_SESSION['username'], 'users' => $user_list); */
/* 	} */
/* } */

function send_message()
{
	if(!isset($_SESSION['username']) || !isset($_SESSION['user_id']))		
	{
		throw new Exception("login please!");
	}
	elseif(!isset($_POST['message']))
	{
		throw new Exception("failed");
	}
	else
	{
		$message = new Message($_SESSION['user_id'], $_POST['message'], $_POST['mes_to']);
		$message -> send_message();
	}

	return array('msg' => 'ok');
}

function quit()
{
	$user = new User();
	$user -> set_offline($_SESSION['user_id']);
	$user -> set_user_online_sum($_SESSION['user_id'], 0);
	session_destroy();
}

try
{
	$method = filter_input(INPUT_GET, 'action');
	if(function_exists($method))
	{
		$result = call_user_func($method);
		echo json_encode(array_merge(array('result' => true), array('msg' => $result)));
	}
	else
	{
		throw new Exception('fetal error!');
	}
}
catch(Exception $e)
{
	echo json_encode(array_merge(array('result' => false), array('msg' => $e -> getMessage())));
}

?>
